package com.xxdh.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.xxdh.domain.dto.WorkDto;
import com.xxdh.domain.po.Course;
import com.xxdh.domain.po.StudentWorkRelation;
import com.xxdh.domain.po.User;
import com.xxdh.domain.po.Work;
import com.xxdh.domain.vo.WorkStudentVo;
import com.xxdh.domain.vo.WorkTeacherVo;
import com.xxdh.mapper.WorkMapper;
import com.xxdh.service.ICourseService;
import com.xxdh.service.IStudentWorkRelationService;
import com.xxdh.service.IUserService;
import com.xxdh.service.IWorkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.Builder;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 作业表 服务实现类
 * </p>
 *
 * @author zwg
 * @since 2024-10-19
 */
@Service
@RequiredArgsConstructor
public class WorkServiceImpl extends ServiceImpl<WorkMapper, Work> implements IWorkService {

    private final IStudentWorkRelationService studentWorkRelationService;
    private final ICourseService courseService;
    private final IUserService userService;
    private final WorkMapper workMapper;

    /**
     * 添加作业
     * @param workDto 作业信息
     */
    @Transactional
    @Override
    public void addWork(WorkDto workDto) {
        // 将workDto转换为work对象
        Work work = BeanUtil.copyProperties(workDto, Work.class);
        // 设置作业所属课程名称
        Course course = courseService.getCourseById(workDto.getCourseId());
        work.setSubject(course.getCourseName());
        // 保存作业
        save(work);
        // 获取课程下的学生id列表
        List<Long> studentIds = courseService.getStudentIdListByCourseId(workDto.getCourseId());
        // 为每个学生添加作业关系
        studentWorkRelationService.addStudentWorkRelationByStudentIdsAndWorkId(studentIds, work.getId());
    }

    /**
     * 根据学生id获取作业列表
     * @param studentId 学生id
     * @return
     */
    @Override
    public List<WorkStudentVo> getWorkListByStudent(Long studentId) {
        // 获取未完成的作业id列表
        List<Long> workIds = studentWorkRelationService.getNotCompletedWorkIdsByStudentId(studentId);
        // 根据作业id列表获取作业列表
        List<Work> works = listByIds(workIds);
        // 将work列表转换为workVo列表
        List<WorkStudentVo> workStudentVos = works.stream().map(work -> BeanUtil.copyProperties(work, WorkStudentVo.class)).toList();
        return workStudentVos;
    }

    /**
     * 根据教师id获取作业列表
     * @param teacherId 教师id
     * @return
     */
    @Override
    public List<WorkTeacherVo> getWorkListByTeacher(Long teacherId) {
        // 获取教师所教课程下的作业列表
        List<Course> courseList = courseService.getCourseListByTeacherId(teacherId);
        if(CollUtil.isEmpty(courseList)){
            return null;
        }
        // 获取课程id列表
        List<Long> courseIds = courseList.stream().map(course -> course.getId()).toList();
        // 根据课程id列表获取作业列表
        List<Work> works = workMapper.selectWorksByCourseIds(courseIds);
        // 将work列表转换为workVo列表
        List<WorkTeacherVo> result = new ArrayList<>(works.size());
        for (Work work : works) {
            WorkTeacherVo workTeacherVo = BeanUtil.copyProperties(work, WorkTeacherVo.class);
            studentWorkRelationService.setWorkSumAndCompleteSum(workTeacherVo);
            result.add(workTeacherVo);
        }
        return result;
    }

    /**
     * 提交作业
     * @param studentId
     * @param workId
     * @param fileAddress
     */
    @Override
    public void submitWork(Long studentId,String workId, String fileAddress) {
        studentWorkRelationService.updateWorkStatus(studentId,workId, fileAddress);
    }

    /**
     * 删除作业
     * @param workId 作业id
     */
    @Transactional
    @Override
    public void deleteWork(String workId) {
        removeById(workId);
        studentWorkRelationService.deleteStudentWorkRelationByWorkId(workId);
    }

    /**
     * 根据作业id获取作业列表
     * @param workId 作业id
     * @return
     */
    @Override
    public List<WorkTeacherVo> getWorkListByWorkId(String workId) {
        // 获取已完成的作业关系列表
        List<StudentWorkRelation> studentWorkRelations = studentWorkRelationService.getCompleteStudentWorkRelationByWorkId(workId);
        if(studentWorkRelations.isEmpty()){
            return null;
        }
        // 将studentWorkRelation列表转换为workTeacherVo列表
        List<WorkTeacherVo> workTeacherVos = new ArrayList<>(studentWorkRelations.size());
        for (StudentWorkRelation studentWorkRelation : studentWorkRelations) {
            User user = userService.getUserById(studentWorkRelation.getStudentId());
            WorkTeacherVo workTeacherVo = WorkTeacherVo.builder().studentName(user.getUserName())
                    .fileAddr(studentWorkRelation.getFileAddr()).build();
            workTeacherVos.add(workTeacherVo);
        }
        return workTeacherVos;
    }
}
